package com.home766.dao.impl;

import com.home766.dao.IDictionaryDao;
import com.home766.dto.DictionaryDTO;
import com.home766.entity.Dictionary;
import com.home766.util.AppendSqlUtil;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.dao.NativeBaseDao;
import net.biz.dao.impl.BaseDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.persistence.FlushModeType;
import javax.persistence.TypedQuery;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DictionaryDaoImpl extends BaseDaoImpl<Dictionary, Long> implements IDictionaryDao{

	@Autowired
	private NativeBaseDao nativeBaseDao;
	
	@Override
	public List<Dictionary> findRoots(Integer count) {
		// TODO Auto-generated method stub
		String jpql = "select dictionary from Dictionary dictionary where dictionary.parent is null order by dictionary.order asc";
		TypedQuery<Dictionary> query = entityManager.createQuery(jpql, Dictionary.class).setFlushMode(FlushModeType.COMMIT);
		if (count != null) {
			query.setMaxResults(count);
		}
		return query.getResultList();
	}

	@Override
	public List<DictionaryDTO> findChildren(Long dictionaryId) {
		// TODO Auto-generated method stub
		String sql = " select dv.id , dv.title ,dv.description  from 766_dictionary dv"
				+ " where dv.parent=:dictionaryId order by if(isnull(dv.orders),1,0),dv.orders asc ";
		Map<String,Object> tempMap = new HashMap<String, Object>();
		tempMap.put("dictionaryId", dictionaryId);
		return nativeBaseDao.findList(sql, tempMap, DictionaryDTO.class);
	}

	@Override
	public Page<Dictionary> findPage(Map<String, Object> params, Pageable pageable) {
		// TODO Auto-generated method stub
		StringBuffer buffer=new StringBuffer(" select * from  766_dictionary where 1 = 1 and parent is null");
		AppendSqlUtil.appendSql(params, buffer, "title,description",null);
		AppendSqlUtil.appendOrdSQL(pageable, buffer, null);
		Page<Dictionary> page= nativeBaseDao.findPage(buffer.toString(), params, pageable, Dictionary.class);
		return page;
	}

	@Override
	public List<Dictionary> findRoots(Boolean bool, Integer count) {
		// TODO Auto-generated method stub
		String jpql = "select dictionary from Dictionary dictionary where dictionary.parent is null ";
		if(bool!=null){
			jpql+=" and dictionary.flag=:bool ";
		}
		jpql+=" order by dictionary.order asc ";
		TypedQuery<Dictionary> query = entityManager.createQuery(jpql, Dictionary.class).setFlushMode(FlushModeType.COMMIT);
		if (count != null) {
			query.setMaxResults(count);
		}
		if(bool){
			query.setParameter("bool", bool);
		}
		return query.getResultList();
	}

	@Override
	public List<DictionaryDTO> findChildren(Long dictionaryId, Boolean bool) {
		// TODO Auto-generated method stub
		String sql = " select dv.id , dv.title ,dv.description  from 766_dictionary dv"
				+ " where dv.parent=:dictionaryId ";
		Map<String,Object> tempMap = new HashMap<String, Object>();
		if(bool!=null){
			sql+=" and dv.flag=:bool ";
			if(bool){
				tempMap.put("bool", 1);
			}else{
				tempMap.put("bool", 0);
			}
		}
		tempMap.put("dictionaryId", dictionaryId);
		return nativeBaseDao.findList(sql, tempMap, DictionaryDTO.class);
	}

	@Override
	public List<Dictionary> findAll() {
		String jpql = "select dictionary from Dictionary dictionary where dictionary.flag=true  order by dictionary.id asc ";
		TypedQuery<Dictionary> query = entityManager.createQuery(jpql, Dictionary.class).setFlushMode(FlushModeType.COMMIT);
		return query.getResultList();
	}

	@Override
	public Dictionary findById(Boolean bool, Long id) {
		// TODO Auto-generated method stub
		String jpql = "select dictionary from Dictionary dictionary where dictionary.flag=:bool  where dictionary.id=:id ";
		TypedQuery<Dictionary> query = entityManager.createQuery(jpql, Dictionary.class).setFlushMode(FlushModeType.COMMIT);
		query.setParameter("bool", bool);
		query.setParameter("id", id);
		query.getSingleResult();
		return query.getSingleResult();
	}

}
